package ms.unicorn.erml.edit.parts;

import ms.unicorn.erml.ERML;
import ms.unicorn.erml.figures.SubPartFigure;
import ms.unicorn.ui.layout.DockConstraint;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
import org.eclipse.gmf.runtime.notation.View;

public class SubPartEditPart extends ContainerEntityEditPart {

	public static final int COLLAPSE_HANDLE_HEIGHT = 30;
	
	public SubPartEditPart(View view) {
		super(view);
	}
	

    /**
     * Returns bounds of figure without border figure included in absolute
     * coordinates .(zoom not computed == DP DevicePixel).
     * 
     * @generated NOT
     * @return
     */
    public Rectangle getAbsBoundsWithoutBorder() {
        SubPartFigure figure = (SubPartFigure)getPrimaryShape();
        Rectangle theBounds = figure.getHandleBounds();
        figure.translateToAbsolute(theBounds);
        return theBounds;
    }
    
    public double getZoom() {
        double zoom = 1.0;
        RootEditPart rootEditPart = getRoot();
        if (rootEditPart instanceof ScalableFreeformRootEditPart) {
            zoom = ((ScalableFreeformRootEditPart) rootEditPart)
                    .getZoomManager().getZoom();
        }
        return zoom;
    }

    @Override
    public SubPartFigure getPrimaryShape() {
    	return (SubPartFigure) super.getPrimaryShape();
    }
  
    @Override
    public void setCollapse(boolean collapsed) {
    	
    	LayoutManager lm = getFigure().getLayoutManager();
    	DockConstraint ncs = (DockConstraint) lm.getConstraint(nameFigure);
    	if (collapsed) {
    		//nameFigure.setAlignment(PositionConstants.CENTER);
    		//ncs.setDock(DockConstraint.DOCK_FILL);
    	}
    	else
    	{
    		//nameFigure.setAlignment(PositionConstants.CENTER);
    		//ncs.setDock(DockConstraint.DOCK_TOP);
    	}
    	super.setCollapse(collapsed);
    }
    
    @Override
    public IFigure getContentPane() {
    	return getPrimaryShape().getBodyCompartmentPlate();
    }

	@Override
	public CompartmentEditPart getCompartmentEditPart() {
		return (CompartmentEditPart) getChildBySemanticHint(ERML.SUBPART_COMPARTMENT_ID);
	}
}
